<template>
  <div>
    <div class="app_card mb-2 pt-4 pb-4">
      <div class="d-flex justify-content-between search-form">
        <div class="flex-grow-1">
          <el-input class="w200px mr-3" v-model.trim="queryParam.recordingIp" autocomplete="off" placeholder="录播设备IP"/>
          <el-button type="primary"  @click="onRefresh()">搜索</el-button>
          <el-button type="primary" plain @click="onReset()">重置</el-button>
        </div>
        <div class="d-flex">
          <el-button type="primary" @click="onAdd()">新建</el-button>
          <el-button type="primary" plain @click="onDeleteMulti()">批量删除</el-button>
          <el-button type="primary" plain @click="onSyncMulti()">批量同步</el-button>
        </div>
      </div>
    </div>
    <div class="app_card pt-4  pb-5">
      <el-table stripe size="large" :data="dataOfTable" height="calc(100vh - 300px)" sortable="custom" @sort-change="handleSortChange" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50"></el-table-column>

        <el-table-column label="录播设备IP" prop="roomRecordingIp" >
          <template #default="scope">{{ scope.row.roomRecordingIp }}</template>
        </el-table-column>
        <el-table-column label="录播设备状态" prop="roomRecordingIp" >
          <template #default="scope">
            <div class="status-red dot" v-if="scope.row.recordingStatus === 1">离线</div>
            <div class="status-green dot" v-if="scope.row.recordingStatus === 2">在线</div>
          </template>
        </el-table-column>
        <el-table-column label="直播地址" prop="recordingIp" >
          <template #default="scope">
            <div>{{ scope.row.roomLiveAddressHdmi }}</div>
            <div>{{ scope.row.roomLiveAddressStu }}</div>
            <div>{{ scope.row.roomLiveAddressStuclose }}</div>
            <div>{{ scope.row.roomLiveAddressTea }}</div>
            <div>{{ scope.row.roomLiveAddressTeaclose }}</div>
            <div>{{ scope.row.roomLiveAddressWbc }}</div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150" align="left">
          <template #default="scope">
<!--            <el-button link type="primary" @click="onEditRow(scope.row)">编辑</el-button>-->
            <el-button link type="primary" @click="onSyncRow(scope.row)">同步直播地址</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mt-2">
        <el-pagination background v-model:currentPage="queryParam.page" v-model:page-size="queryParam.pageSize" :page-sizes="paginationOption.pageSizes" :background="false" :layout="paginationOption.layout" :total="paginationOption.count" @size-change="handleChangeSize" @current-change="handleChangeCurrent" />
      </div>
    </div>
    <EditDialog ref="editDialogRef" @success="loadData()"></EditDialog>
  </div>
</template>

<script setup>
/**
 * 角色组件
 */
import { ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';
//组件
import EditDialog from './EditDialog.vue';

// ======================================================
//                      变量声明
// ======================================================

// 组件引用
const editDialogRef = ref(null);
// 表格数据
const dataOfTable = ref([]);
// 组件可视状态
const visibleState = ref({
  searchForm: true,
})
// 表格查询参数
const queryParam = ref({
  recordingIp: '',
  page: 1,
  pageSize: 10,
})
// 分页栏配置数据
const paginationOption = ref({
  layout: 'total, sizes, prev, pager, next, jumper',
  pageSizes: [10, 20, 50, 100, 500, 1000],
  count: 0,
})
// 表格多选数据
const multipleSelection = ref([]);

// ======================================================
//                      点击操作
// ======================================================
// 点击刷新按钮
const onRefresh = () => {
  queryParam.value.page = 1;
  loadData();
}

//点击重置按钮
const onReset = () => {
  queryParam.value ={
  recordingIp: '',
  page: 1,
  pageSize: 10,
}
  loadData()
}

// 点击新增按钮
const onAdd = () => {
  editDialogRef.value.show(null, '添加');
}



// 点击批量删除按钮
const onDeleteMulti = () => {
  let n = multipleSelection.value.length, ip = multipleSelection.value.map(v => v.roomRecordingIp).toString();
  if (n) {
    ElMessageBox.confirm("确定要删除 " + n + " 条记录吗？", "警告").then(() => {
      EsAjax.delete(ApiUrls.recordboxDelete, { data: { ip } }).then(res => {
        if (res.data.code !== 200) {
          ElMessage.warning(res.data.msg);
          return;
        }
        ElMessage.success("操作成功");
        loadData();
      }).catch(err => {
        ElMessage.error("网络错误：" + err.message);
      })
    }).catch(() => { })
  } else {
    ElMessage.info("请选择批量数据");
  }

}

// 点击行内编辑按钮
const onEditRow = (row) => {
  editDialogRef.value.show(row.roomRecordingIp, '编辑类型', row);
}

// 批量同步按钮
const onSyncMulti = () => {
  let n = multipleSelection.value.length, ip = multipleSelection.value.map(v => v.roomRecordingIp).toString();
  if (n) {
    ElMessageBox.confirm("确定要同步地址吗？", "警告",{
      beforeClose: (action, instance, done) => {
        if (action === 'confirm') {
          instance.confirmButtonLoading = true
          EsAjax.post(ApiUrls.recordboxChange,  { ip })
              .then(res => {
                if (res.data.code !== 200) {
                  ElMessage.warning(res.data.msg);
                  return;
                }
                loadData();
                ElMessage.success('操作成功');
              })
              .catch(err => { ElMessage.error("网络错误：", err.message) })
              .finally(()=>{instance.confirmButtonLoading = false;done();});
        } else {
          done()
        }
      }
    })
  } else {
    ElMessage.info("请选择批量数据");
  }
}
// 点击行内同步按钮
const onSyncRow = (row) => {
  ElMessageBox.confirm("确定要同步直播地址吗？", "警告",{
    beforeClose: (action, instance, done) => {
      if (action === 'confirm') {
        instance.confirmButtonLoading = true
        EsAjax.post(ApiUrls.recordboxChange,  { ip: row.roomRecordingIp })
            .then(res => {
              if (res.data.code !== 200) {
                ElMessage.warning(res.data.msg);
                return;
              }
              loadData();
              ElMessage.success('操作成功');
            })
            .catch(err => { ElMessage.error("网络错误：", err.message) })
            .finally(()=>{instance.confirmButtonLoading = false;done();});
      } else {
        done()
      }
    }
  })
}


// ======================================================
//                      处理事件
// ======================================================

// 点击行内删除按钮
const onDeleteRow = (row) => {
  ElMessageBox.confirm("确定要删除该记录吗？", "警告").then(() => {
    EsAjax.delete(ApiUrls.recordboxDelete, { data: { ip: [row.roomRecordingIp] } })
      .then(res => {
        if (res.data.code !== 200) {
          ElMessage.warning(res.data.msg);
          return;
        }
        loadData();
      })
      .catch(err => { ElMessage.error("网络错误：", err.message) });
  }).catch(() => {

  });
}

// 处理表格全选
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

// 处理表格排序
const handleSortChange = (column) => {
  queryParam.value.orderby = column.prop + ' ' + (column.order == 'descending' ? 'desc' : 'asc');
  console.log(queryParam.value);
  loadData();
}

// 处理分页尺寸
const handleChangeSize = (val) => {
  console.log(`${val} items per page`);
  queryParam.value.page = 1;
  queryParam.value.pageSize = val;
  loadData();
}

// 处理页码
const handleChangeCurrent = (val) => {
  console.log(`current page: ${val}`)
  queryParam.value.page = val;
  loadData();
}

// ======================================================
//                      处理事件
// ======================================================
const loadData = () => {
  EsAjax.post(ApiUrls.recordboxList, queryParam.value).then(res => {
    if (res.data.code !== 200) {
      ElMessage.warning(res.data.msg);
      return;
    }
    let { list, count } = res.data.data
    dataOfTable.value = list;
    paginationOption.value.count = count;
  }).catch(err => {
    ElMessage.error(err.message);
  })
}

loadData();
</script>

